package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.yangzihui.NewMarketAd;
import com.cskaoyan.bean.yangzihui.ResponseOfNewMarketAd;
import com.cskaoyan.config.zhangshizhi.CountTime;
import com.cskaoyan.service.AdService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author yangzihui
 * @Description 推广管理模块中的广告管理的controller
 * @Create 2022-06-26 13:36
 * @Version
 */
@RestController
@RequestMapping("admin/ad")
public class AdController {

    @Autowired
    AdService adService;

    /**
     * 对广告的显示和查找
     *
     * @param baseParam
     * @param name
     * @param content
     * @return
     */
    @CountTime
    @RequiresPermissions(value = {"admin:ad:list", "*"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo adList(BaseParam baseParam, String name, String content) {
        CommonData commonData = adService.query(baseParam, name, content);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 对广告的编辑
     *
     * @param marketAd
     * @return
     */
    @CountTime
    @RequiresPermissions(value = {"admin:ad:update", "*"}, logical = Logical.OR)
    @PostMapping("update")
    public BaseRespVo adUpdate(@RequestBody MarketAd marketAd) {
        if (marketAd.getLink() == null) {
            return BaseRespVo.invalidParameter("参数不能为空");
        }
        MarketAd ads = adService.update(marketAd);
        return BaseRespVo.ok(ads);
    }

    /**
     * 对广告的删除
     *
     * @param marketAd
     * @return
     */
    @CountTime
    @RequiresPermissions(value = {"admin:ad:delete", "*"}, logical = Logical.OR)
    @PostMapping("delete")
    public BaseRespVo adDelete(@RequestBody MarketAd marketAd) {
        adService.delete(marketAd);
        return BaseRespVo.ok(marketAd);
    }

    /**
     * 对广告的添加
     *
     * @param newMarketAd
     * @param bindingResult
     * @return
     */
    @CountTime
    @RequiresPermissions(value = {"admin:ad:create", "*"}, logical = Logical.OR)
    @PostMapping("create")
    public BaseRespVo adCreate(@Valid @RequestBody NewMarketAd newMarketAd, BindingResult bindingResult) {
        if (newMarketAd.getLink() == null) {
            return BaseRespVo.invalidParameter("参数不能为空");
        }
        if (bindingResult.hasFieldErrors()) {
            return BaseRespVo.invalidParameter("活动链接格式不正确，应为纯数字");
        }
        // 查询活动链接商品是否存在
        Integer num = adService.selectGoodsId(newMarketAd);
        if (num == 0) {
            return BaseRespVo.invalidParameter("该商品不存在");
        }
        ResponseOfNewMarketAd responseOfNewMarketAd = adService.create(newMarketAd);
        return BaseRespVo.ok(responseOfNewMarketAd);
    }
}
